package behaviour.building;

import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class ReceiveConflictBehaviour extends Behaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = -223487643325025689L;

	private int step = 0;
	
	@Override
	public void action() {
		// Matching avec les messages
		MessageTemplate mt = MessageTemplate.MatchConversationId("in-conflict");
		ACLMessage msgConflict = myAgent.receive(mt);
		if(msgConflict != null) {
			
			System.out.println(" + + + Receiving conflit : " + myAgent.getName() + " - Etape : " + step);
			
			switch(step) {
			
			case 0:
				
				myAgent.addBehaviour(new ResolveConflictBehaviour(msgConflict.getContent()));
				++step;
				break;
			
			case 1:
				
				myAgent.addBehaviour(new RequestMoveBehaviour(msgConflict.getContent(), 0, "", myAgent.getAID()));
				++step;
				break;
				
			case 2:
				
				myAgent.doDelete(); //terminaison de l'agent
				++step;
				break;
			}
		}
		else
			block();

	}

	@Override
	public boolean done() {
		return step == 3;
	}

}
